
//
//  PGStatusCellTopView.swift
//  PGWeibo
//
//  Created by Apple on 15/11/9.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SDWebImage


class PGStatusCellTopView: UIView {
    /// 微博的视图模型
    var viewModel: PGStatusViewModel? {
        didSet{

            iconView.sd_setImageWithURL(viewModel?.iconImageURL, placeholderImage: UIImage(named: "avatar_default_big"), options: [.RetryFailed, .RefreshCached])
            nameLabel.text = viewModel?.status.user?.screen_name
            mbRankView.image = viewModel?.mbRankImage
            verifiedView.image = viewModel?.verifiedImage
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 懒加载
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.cornerRadius = 17.5
        iv.layer.masksToBounds = true
        return iv
    }()
    private lazy var verifiedView: UIImageView = UIImageView()
    private lazy var nameLabel: UILabel = UILabel(title: "魔兽世界", textFont: 14, color: UIColor.blackColor())
    private lazy var mbRankView: UIImageView = UIImageView()
    private lazy var timeLabel: UILabel = UILabel(title: "刚刚", textFont: 11, color: UIColor.orangeColor())
    private lazy var fromLabel: UILabel = UILabel(title: "来自 从马桶捡来的iPhone6s", textFont: 11)
}
// MARK: - 设置控件
extension PGStatusCellTopView {
    private func setupUI() {
        let sepView = UIView()
        sepView.backgroundColor = UIColor(white: 0.96, alpha: 1)
        addSubview(sepView)
        
        //添加控件
        addSubview(iconView)
        addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(mbRankView)
        addSubview(timeLabel)
        addSubview(fromLabel)
        
        //分割视图的约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(statusCellMargin)
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
        }
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
//            make.topMargin.equalTo(statusCellMargin)
//            make.leadingMargin.equalTo(statusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.width.equalTo(statusIconWidth)
            make.height.equalTo(statusIconWidth)
        }
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right).offset(-verifiedView.bounds.width/4)
            make.centerY.equalTo(iconView.snp_bottom).offset(-verifiedView.bounds.width/4)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
        }
        mbRankView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
        }
        fromLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
        }
    }
}
